package org.wattdepot.cli.commands;

//import java.util.List;
import java.util.List;
import org.wattdepot.cli.UserCommandInterface;
import org.wattdepot.client.WattDepotClientException;
import org.wattdepot.resource.property.jaxb.Properties;
import org.wattdepot.resource.source.jaxb.Source;

/**
 * Prints out the fuel type from the source.
 * 
 * @author Jarret Mizo, Shaun Ramento
 * 
 */
public class FuelTypes extends UserCommandInterface implements UserCommands {

  /**
   * Prints information about a single source.
   * 
   * @param args arguments from command line.
   * @return output prints out the result of FuelTypes.
   */
  @Override
  public String doCommand(String args) {
    String[] inputString = args.split(" ");
    String output = null;
    try {
      output = fuelType(inputString[1]);
    }
    catch (WattDepotClientException e) {
      output = "Cannot recognize source\n";
    }
    return output;
  }

  /**
   * Prints information about a single source.
   * 
   * @param sourceName name of the source.
   * @return buffer prints out the result of FuelTypes.
   * @throws WattDepotClientException throws exception.
   */
  public String fuelType(String sourceName) throws WattDepotClientException {
    StringBuffer buffer = new StringBuffer(255);

    Source source = getClient().getSource(sourceName);

    if (sourceExists(sourceName)) {

      int i = 0;
      if (source.isVirtual()) {
        List<String> urlList = source.getSubSources().getHref();

        int urlListSize = urlList.size();
        buffer.append("fueltypes " + sourceName + "\n");
        while (i != urlListSize) {
          String urlName = urlList.get(i);
          int lastIndex = urlName.lastIndexOf('/');
          String name = urlName.substring(lastIndex + 1);
          Source subSource = getClient().getSource(name);
          if (subSource.isVirtual()) {
            i++;
            buffer.append(fuelType(name));

          }
          else {
            buffer.append(nonVirtual(name));
            i++;
          }
        }
      }
      else {

        buffer.append("Cannot desfine if virtual or non-virtual\n");
      }
    }
    return buffer.toString();
  }

  /**
   * Prints information about a single source.
   * 
   * @param sourceName sourceName is for non-virtual source.
   * @return buffer prints out the fueltype of the sourceName.
   * @throws WattDepotClientException throws exception.
   */
  public String nonVirtual(String sourceName) throws WattDepotClientException {
    StringBuffer buffer = new StringBuffer();
    Properties property = getClient().getSource(sourceName).getProperties();
    String propertyString = property.toString();
    int propertyStringLength = propertyString.length();
    int value = propertyString.lastIndexOf("value");
    String fuelType = propertyString.substring(value + 6, propertyStringLength - 2);
    buffer.append(fuelType + " : " + sourceName + "\n");
    return buffer.toString();
  }

}
